#include "NavigationEdge.h"
#include "include.h"
#include "CharacterObject.h"

NavigationEdge::NavigationEdge( TextureManager * textureManager, Vertex * rightEndpoint, Vertex * leftEndpoint)
:Object(textureManager,NULL)
{
	this->leftEndpoint = leftEndpoint;
	this->rightEndpoint = rightEndpoint;
	
	vertices = new VertexList(leftEndpoint);
	vertices->Add(rightEndpoint);
	CreatePropertyMenu();
	propertyMenu->SetFormContent(0,"walk");
	propertyMenu->SetFormContent(1,"walk");
	propertyMenu->SetFormContent(2,"wander,stand");
	propertyMenu->Hide();
	parent = NULL;
}
NavigationEdge::NavigationEdge(	TextureManager * textureManager,Vertex * rightEndpoint, Vertex * leftEndpoint, char * rightToLeft,
				char * leftToRight, char ** behaviorTags, int numTags)
:Object(textureManager,NULL)
{
	this->leftEndpoint = leftEndpoint;
	this->rightEndpoint = rightEndpoint;
	
	vertices = new VertexList(leftEndpoint);
	vertices->Add(rightEndpoint);
	
	CreatePropertyMenu();		
	propertyMenu->SetFormContent(0, rightToLeft);
	propertyMenu->SetFormContent(1, leftToRight);
	int size = 2;
	for(int i = 0; i < numTags; i++)
	{
		size += strlen(behaviorTags[i]);
	}
	char * behaviorString = (char*)malloc(size + (numTags*2 - 1));
	behaviorString[0] = '\0';
	for(int i = 0; i < numTags; i++)
	{
		printf("pre behaviorString = %s\n", behaviorString);
		if(strcmp(behaviorString, "")==0)
			sprintf(behaviorString, "%s", behaviorTags[i]);
		else
			sprintf(behaviorString, "%s,%s", behaviorString, behaviorTags[i]);
		printf("post behaviorString = %s\n", behaviorString);
	}
	
	propertyMenu->SetFormContent(2,behaviorString);
	propertyMenu->Hide();		
	parent = NULL;
}
NavigationEdge::~NavigationEdge()
{
}
void NavigationEdge::AddVertex(Vertex * vertex)
{
	if(parent)
	{
		NavigationEdge * edge = NULL;
		if((vertex->GetPosition() | rightEndpoint->GetPosition()) < 
			(vertex->GetPosition() | leftEndpoint->GetPosition()))
		{
			edge = new NavigationEdge(textureManager, vertex, rightEndpoint);
			edge->SetParent(parent);
			
		}
		else
		{
			edge = new NavigationEdge(textureManager, leftEndpoint, vertex);
			edge->SetParent(parent);
		}
		if(edge)
			parent->AddEdge(edge);
	}
}
void NavigationEdge::AddVertexBefore(Vertex * vertex, Vertex * before)
{
}
void NavigationEdge::RemoveVertex(Vertex * vertex)
{
	if(parent)
		parent->RemoveVertex(vertex);
}
bool NavigationEdge::CheckObjectHover(Mouse * mouse)
{
	Vector2D mouseSpot = mouse->GetWorldPosition();
	Vector2D ep1 = rightEndpoint->GetPosition();
	Vector2D ep2 = leftEndpoint->GetPosition();
	float dist = Vector2D::PointToLineDistance(mouseSpot, ep1, ep2);
	if(dist < 1.0)
	{
		activeObject = true;
	}
	else
	{
		activeObject = false;
	}
	return activeObject;
}
void NavigationEdge::CreatePropertyMenu()
{
	printf("NavigationEdge::CreatePropertyMenu\n");
	propertyMenu = new FormMenu(textureManager, "3,Behavior,1,LeftToRight,1,RightToLeft,1", PROPERTY_MENU_POSITION, 0);
		
}
Vertex * NavigationEdge::GetRightEndpoint()
{
	return rightEndpoint;
}
Vertex * NavigationEdge::GetLeftEndpoint()
{
	return leftEndpoint;
}
Vector2D NavigationEdge::GetRotationPoint()
{
	return (rightEndpoint->GetPosition() + leftEndpoint->GetPosition())	* 0.5;
}
void NavigationEdge::ViewProperties()
{
	propertyMenu->Show();
}
void NavigationEdge::HideProperties()
{
	propertyMenu->Hide();
}
void NavigationEdge::SetParent(CharacterObject * parent)
{
	this->parent = parent;
}
char * NavigationEdge::GetTag()
{
	char * rightTag = rightEndpoint->GetTag();
	char * leftTag = leftEndpoint->GetTag();
	char * r2lLoco = propertyMenu->GetFormContent(0);
	char * l2rLoco = propertyMenu->GetFormContent(1);
	char * behaviorString = propertyMenu->GetFormContent(2);
	printf("Here1\n");
	
	int numTags = 0;
	int len = 0;
	if(behaviorString)
	{
		len = strlen(behaviorString);
		numTags++;
		for(int i = 0; i < len; i++)
		{
			printf("Here1.2\n");
			if(behaviorString[i] == ',')
				numTags++;
		}
	}
	printf("Here2\n");
	char * tag = (char *)malloc(sizeof(char)*(strlen(rightTag) + strlen(leftTag) + 
											  strlen(r2lLoco) + strlen(l2rLoco)+ len + 14));
	if(behaviorString)
	{
		printf("bah\n");
		sprintf(tag, "%s, %s, %s, %s, %d, %s", rightTag, leftTag, r2lLoco, l2rLoco, numTags, behaviorString);
		free(behaviorString);
	}
	else
	{
		sprintf(tag, "%s, %s, %s, %s, %d", rightTag, leftTag, r2lLoco, l2rLoco, numTags);
	}
	free(l2rLoco);
	free(r2lLoco);
	free(rightTag);
	free(leftTag);
	
	printf("NavigationEdgeTag = %s\n", tag);
	return tag;
}
void NavigationEdge::AddRightEdge(Vertex * endpoint)
{
	printf("NavigationEdge::AddRightEdge\n");
	if(rightEndpoint != endpoint)	
	{
		printf("Adding right edge\n");
		if(rightEndpoint && endpoint)
		{
		 	vertices->Remove(rightEndpoint);
		 	rightEndpoint = endpoint;
		 	vertices->Add(endpoint);
		}
		
	}
}
void NavigationEdge::AddLeftEdge(Vertex * endpoint)
{
	if(leftEndpoint != endpoint)	
	{
		printf("Adding left edge\n");
		if(leftEndpoint && endpoint)
		{
			vertices->Remove(leftEndpoint);
			//delete(leftEndpoint);
			leftEndpoint = endpoint;
			vertices->Add(endpoint);
		}	
	}
}
Object * NavigationEdge::GetParent()
{
	return parent;
}
